package com.glorysoft.hlgdpda.ui.materialandtool.viewmodel

import android.widget.TextView
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.entity.CarrierEntity
import com.glorysoft.hlgdpda.entity.CodeListEntity
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.model.TaskRepository
import com.glorysoft.hlgdpda.ui.trackout.adapter.DefectListAdapter
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: ScrapViewModel
 * Author: XJH
 * Date: 2021/8/20 16:29
 * Description:
 * History:
 */
class ScrapViewModel : BaseViewModel() {

    val carrierList: List<String> = listOf("批次号", "产品名称", "数量", "工步名称", "载具ID", "状态", "工单号")
    val defectList: List<String> = listOf("No.", "不良代码", "不良描述", "数量", "操作")

    var defectLists = mutableListOf<CodeListEntity.CodeEntity>()
    private val trackOutRepository = TaskRepository()
    val defectCodeList = mutableListOf<String>()
    var lotEntity = MutableLiveData<LotEntity>()

    //载具列表
    var carrierUpItems = ObservableArrayList<LotEntity>()
    var carrierUpItemsBinding =
        ItemBinding.of<LotEntity>(BR.carrierItem, R.layout.item_carrier)

    private val defectAdapter: DefectListAdapter =
        DefectListAdapter(defectLists, object : DefectListAdapter.OnDeleteListener {
            override fun setOnDeleteListener(item: CodeListEntity.CodeEntity) {
                defUI.callObserve.value = CallObserveBean(1, item)
            }

        })

    fun initDefectAdapter(recyclerView: RecyclerView, textView: TextView) {
        recyclerView.adapter = defectAdapter
        defectAdapter.textView = textView
        defectLists.clear()
    }

    fun scrap(){
        if (lotEntity.value == null) return
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "SCRAP")
        map.addProperty("ACTIONCODE", "SCRAP")
        map.addProperty("ACTIONREASON", "报废")
        map.addProperty("LOTID", lotEntity.value!!.LOT.LOTID)
        map.addProperty("DURABLEID", lotEntity.value!!.LOT.DURABLEID)
        val defectList = arrayListOf<CodeListEntity.ActionEntity>()
        if (defectAdapter.data.size > 0) {
            defectAdapter.data.forEach {
                val entity = CodeListEntity.ActionEntity()
                entity.ACTIONTYPE = "Scrap"
                entity.ACTIONCODE = it.KEYID
                entity.MAINQTY = it.MAINQTY
                entity.LOTID = lotEntity.value?.LOT?.LOTID.toString()
                defectList.add(entity)
            }
        }
        map.add("LOTACTIONLIST", JsonParser().parse(Gson().toJson(defectList)).asJsonArray)
        launchOnlyResult({

            trackOutRepository.scrap(map)
            defUI.toastEvent.value = ToastMessageBean("报废成功", true)
            carrierUpItems.clear()
            defectAdapter.data.clear()
            defectAdapter.notifyDataSetChanged()
            defectCodeList.clear()
            defUI.startActivity.call()
        })
    }

    /**
     * 获取批次信息
     */
    fun searchLotInfo(lot: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOTINFOBYID")
        map.addProperty("LOTID", lot)
        launchOnlyResult({
            lotEntity.value = trackOutRepository.getLotInfo(map)
            if (lotEntity.value != null && lotEntity.value!!.LOT.MAINQTY != "") {
                defectAdapter.TotalNum = lotEntity.value!!.LOT.MAINQTY.toInt()
            } else {
                defectAdapter.TotalNum = 0
            }
            defUI.callObserve.value = CallObserveBean(200, lotEntity.value!!)
            getCodeList()

        })
    }

    fun addItem(code: String) {
        val entity = CodeListEntity.CodeEntity()
        entity.KEYID = code.split("/")[0]
        entity.DESCRIPTION = code.split("/")[1]
        defectLists.add(entity)
        defectAdapter.notifyDataSetChanged()
    }


    private fun getCodeList() {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "BYLOT")
        map.add("LOT", JsonParser().parse(Gson().toJson(lotEntity.value!!.LOT)).asJsonObject)
        launchOnlyResult({
            val codeList = trackOutRepository.getCodeList(map)
            defectCodeList.clear()
            if (codeList.DEFECTCODELISTS.isNotEmpty()) {
                codeList.DEFECTCODELISTS.forEach {
                    //代码值和描述
                    defectCodeList.add(it.KEYID + "/" + it.DESCRIPTION)
                }
            } else {
                codeList.SCRAPCODELISTS.forEach {
                    //代码值和描述
                    defectCodeList.add(it.KEYID + "/" + it.DESCRIPTION)
                }
            }

        })

    }

    fun searchCarrierInfo(carrier: String) {
        val map = JsonObject()
        map.addProperty("CARRIERID", carrier)
        launchOnlyResult({
            val lotInfo = trackOutRepository.getLotInfoByCarrier(map)
            carrierUpItems.clear()
            carrierUpItems.add(lotInfo)
            lotEntity.value = lotInfo
            defUI.callObserve.value = CallObserveBean(200, lotInfo)

            getCodeList()
            if (lotEntity.value!!.LOT.MAINQTY != "") {
                defectAdapter.TotalNum = lotEntity.value!!.LOT.MAINQTY.toInt()
            } else {
                defectAdapter.TotalNum = 0
            }

        })
    }

    fun deleteInfo(type: String, item: Any) {
        when (type) {
            "defect" -> {
                defectAdapter.data.remove(item as CodeListEntity.CodeEntity)
                defectAdapter.notifyDataSetChanged()
            }
        }
    }
}